/**********************************************************************/
/*
Author: Karan Makkar, adapted from Michelle's code
Created: Nov 2023
Updated: Aug 2025, by Youssef Assarssah
Description: Master program to run and output 2022 Online Survey outcomes regressions
Output: Tables 6, 7, A17, A18, A30, A31, A32
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_survey_outcomes_master.txt", text replace

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Regression programs
include "$KP_do/3 Functions/impact_regression_programs"

* Table formatting and flipping programs
include "$KP_do/3 Functions/impact_table_programs"

/*----------------------------------------------------*/
* Section: Outcome variable lists
/*----------------------------------------------------*/
              
* Main Table Vars
global vars_22_main tot_cons_adj ///
        ladder_now ///
        any_buy ///
        any_sell ///
		net_ast_buy_w_thous ///
        loan_borrow ///
        loan_get ///
        loan_give ///
        consmooth_move ///
        employed ///
        self_emp_bus_owner ///
        wage_adj ///
        income_adj ///
        job_search ///
        hh_new_bus ///
        job_satisfy_n ///
        business_ability

* Training Vars
global vars_22_training ever_win_47 ///
        receive_kp ///
        take_courses ///
        course_hours ///
        
* Other Vars
global vars_22_other depress_disint_n ///
        efficacy_n ///
        job_web ///
        ewallet_buy ///
        choose_ewallet ///
        job_search_online ///
        views_gov_help_n ///
        views_allocation_n

* Asset Vars
global vars_22_assets bike_buy ///
                    devices_buy ///
                    gold_buy ///
                    livestock_buy ///
                    bike_sell /// 
                    devices_sell ///
                    gold_sell ///
                    livestock_sell

/*----------------------------------------------------*/
* Section: Regression Specifications
/*----------------------------------------------------*/

* SE CLUSTERING (OLS): Individual level
global vce vce(cluster anon_id4)

* INSTRUMENT (IV): win in batch
global instrument_iv win_in_batch

* SE CLUSTERING (IV): Individual level
global cluster cluster(anon_id4)

* FIXED EFFECTS: Strata and inputs into PMO-assigned weight interacted by batch
global absorb absorb(strata gender#batch urban#batch ed_cat#batch)

* FIXED EFFECTS: Inputs into PMO-assigned weight interacted by batch
global demog_batch absorb(gender#batch urban#batch ed_cat#batch)

* FIXED EFFECTS (interacted with female): Inputs into PMO-assigned weight interacted by batch
global demog_female absorb(gender#batch urban#batch ed_cat#batch)  

* FIXED EFFECTS (interacted with urban): Inputs into PMO-assigned weight interacted by batch
global demog_urban absorb(gender#batch#urban urban#batch ed_cat#batch#urban)

* FIXED EFFECTS (interacted with java): Inputs into PMO-assigned weight interacted by batch
global demog_java absorb(gender#batch#java urban#batch#java ed_cat#batch#java)  

* FIXED EFFECTS (interacted with above_hs): Inputs into PMO-assigned weight interacted by batch
global demog_abovehs absorb(gender#batch#above_hs urban#batch#above_hs ed_cat#batch)  

* FIXED EFFECTS (interacted with young): Inputs into PMO-assigned weight interacted by batch
global demog_young absorb(gender#batch#young urban#batch#young ed_cat#batch#young)  

* FIXED EFFECTS (interacted with numapp): Inputs into PMO-assigned weight interacted by batch
global demog_numapp absorb(gender#batch#numapp_xt urban#batch#numapp_xt ed_cat#batch#numapp_xt)  

* FIXED EFFECTS: Strata only
global absorb_strata absorb(strata)

* FIXED EFFECTS: Strata win probability only
global absorb_winprob absorb(stratum_win_prob)

* SAMPLE RESTRICTIONS: after batch 7
global after_batch_7 batch >= 7  

* SAMPLE RESTRICTIONS: after batch 7
global after_batch_18 batch >= 18

* SAMPLE RESTRICTIONS: after batch 7 and before batch 11 (for comparison to 2022 survey)
global batch_7_11 batch >= 7 & batch <= 11  

* COMPLETED SURVEY RESTRICTION:
global and_completed & completed == 1 & consented == 1 & verified == 1

/*----------------------------------------------------------------*/
* Section: Data Setup
/*----------------------------------------------------------------*/

* Save Batch Announcement Date Tempfile
import excel "$KP_deid_admin/Raw/00 Kartu Prakerja Batch Schedule.xlsx", sheet("Batch opening schedule") firstrow clear

replace batch = subinstr(batch, "*", "", .)
destring batch, replace

keep batch announcement

drop if mi(batch)

format announcement %td

tempfile batch_dates
sa `batch_dates', replace

*Save num applications tempfile for 2021
use "${KP_deid_admin}/Clean/pmo_b1-22_clean_long_deid.dta", clear
keep if batch <=17
collapse (count) num_app = batch, by(anon_id4)
tempfile numapp
sa `numapp', replace

* Load 2021 data
use "${KP_deid_survey}/2021/Clean/071323_survey_21_deid_clean_merged.dta", clear
di _N 
keep if completed == 1 & consented == 1 & verified == 1 // need to figure out how to incorporate this into lee bounds later
di _N 

*Num appl var
fmerge 1:1 anon_id4 using `numapp', assert(2 3) keep(3) nogen

gen round = 2021

*Winsorize hours course
winsor2 course_hours, replace cut(0 99)

* Append 2022 data
append using "${KP_deid_survey}/2022/Clean/072125_survey_21_22_deid_clean_control_merged.dta"
keep if completed == 1 & verified == 1 & consented == 1 

mdesc strata gender batch urban education age multi_apply_hh

replace round = 2022 if mi(round)

*Num appl var
bysort anon_id4 round: replace num_app = _N if round == 2022

* Merge in batch announcement dates
fmerge m:1 batch using `batch_dates', assert(2 3) keep(3) nogen

* Batch after 2021 survey dummy
gen startdateDay_21 = startdateDay if round ==2021
bysort anon_id4: gegen startdateDay_21 = max(startdateDay_21), replace

gen batch_after_s21 = startdateDay_21 < announcement

* Cash before survey dummy 
bysort anon_id4: gegen date_incentive = max(date_incentive), replace
gen cash_before_survey = 0
replace cash_before_survey = 1 if date_incentive <= startdateDay
replace cash_before_survey = 1 if ever_win_22 == 0 & ever_win_47 ==1

*Winsorize hours course
gen course_hours22 = course_hours if round ==2022
winsor2 course_hours22, replace cut(0 99)
replace course_hours = course_hours22 if round == 2022
drop course_hours22

// Generate total value of assets purchased/sold (KA02, KA06, KA10, KA14)
* Handl NA, if indv didn't buy/sell (0) and it's NA if they buy and still missing 

preserve 

* Keep only 2022 sample 
keep if round == 2022

* Calculate at the individual level 
duplicates drop anon_id4 bike_* devices_* gold_* livestock_*, force
distinct anon_id4 // 3945
  
* Replace asset values with 0 if the indiv didn't buy/sell 
local asset_types bike devices gold livestock

foreach type in `asset_types' {
    foreach action in buy sell {
    	foreach yr in "" "_21" {
        local var "`type'_`action'"
	
        replace `var'_val`yr' = 0 if `var'`yr' == 0
	
	local `action'_val_vars`yr' ``action'_val_vars`yr'' `type'_`action'_val`yr'
	}
    }
}

* Generate the sum variables
foreach action in buy sell {
	foreach yr in "" "_21" {
		egen tot_ast_`action'`yr' = rowtotal(``action'_val_vars`yr''), mis
	}
}

rename (tot_ast_sell tot_ast_sell_21) (tot_ast_sold tot_ast_sold_21)

* Winsorize total asset vars at 99.5th percentile
winsor2 tot_ast_buy* tot_ast_sold*, cuts(0 99.5) suffix(_w)

* Rename 2021 Win variables to fit into the regression program 
rename (tot_ast_buy_21_w tot_ast_sold_21_w) (tot_ast_buy_w_21 tot_ast_sold_w_21)
 
* Generate Net asset puchases
foreach suff in "" "_w" "_21" "_w_21" {
	gen net_ast_buy`suff'  = tot_ast_buy`suff'  - tot_ast_sold`suff'
	gen net_ast_sold`suff' = tot_ast_sold`suff' - tot_ast_buy`suff' 
}

* Generate thousand scaled vars 
local thous_vars tot_ast_buy tot_ast_sold tot_ast_buy_w tot_ast_sold_w /// 
               net_ast_buy net_ast_buy_w  

foreach var of local thous_vars {
	foreach yr in "" "_21" {
		cap drop `var'_thous`yr'
		gen `var'_thous`yr' = `var'`yr'/1000
		local vars_thous `vars_mil' `var'_thous`yr'
	}
	* Label new variable 
	if !regexm("`var'", "_21$") {
		local label : variable label `var'
		dis "`label'"
		label variable `var'_thous "`label' (thousands IDR)"
	}
}

tempfile asset_data
save `asset_data'
restore 
 
* Merge back with person-batch level data 
merge m:1 anon_id4 using `asset_data', gen(merge_asset)

* X VAR: Generate placeholder
gen x = 0
la var x "Treated"

*Education cat
recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

cap drop age_cat
recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

* Cleaning (move to cleaning file later)
replace yrs_school = 13 if yrs_school==13.5
replace temp_no_work = . if missing(employed)

* Var for constant_reg function
gen female = gender ==0
gen constant = 1
gen missing =.

* Drop non-randomized batches
drop if inlist(batch, 1, 15)
/*----------------------------------------------------------------*/
* Section: 2022 Main IV Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22 ivc_22
global column_num 2

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_main

* loop through each variable of table
foreach y of varlist $vars {

    replace x = ever_win_47
    * No Controls
    local c "stratum_win_prob c.stratum_win_prob#i.batch"
    iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * With Controls
    replace `y'_21 = 0 if batch_after_s21 ==0
    local c "stratum_win_prob c.stratum_win_prob#i.batch `y'_21 batch_after_s21"

    iv_reg `y'_ivc_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * Add models to list
    global model_list $model_list `y'_iva_22 `y'_ivc_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/Table7.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/Table7" "tot_cons_adj"

/*----------------------------------------------------------------*/
* Section: 2022 Main OLS Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns ols_22
global column_num 1

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_main

* loop through each variable of table
foreach y of varlist $vars {

    * Controls local
    local c "stratum_win_prob c.stratum_win_prob#i.batch"

    replace x = win_in_batch
    ols_reg `y'_ols_22 `y' x "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$vce" // (batch 2-22)

    * Add models to list
    global model_list $model_list `y'_ols_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA30.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/TableA30" "tot_cons_adj"

/*----------------------------------------------------------------*/
* Section: 2022 Training Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22
global column_num 1

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_training

* loop through each variable of table
foreach y of varlist $vars {
    
    * Controls Local
    local c "stratum_win_prob c.stratum_win_prob#i.batch"

    * OLS
    if "`y'" == "ever_win_47" {
        replace x = win_in_batch
        ols_reg `y'_iva_22 `y' x "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$vce" // (batch 2-22)
    }

    * IV
    if "`y'" != "ever_win_47" {
        replace x = ever_win_47
        iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)
    } 

    * Add models to list
    global model_list $model_list `y'_iva_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/Table6.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/Table6" "ever_win_47"

/*----------------------------------------------------------------*/
* Section: 2022 Other IV Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22 ivc_22
global column_num 2

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_other

* loop through each variable of table
foreach y of varlist $vars {

    replace x = ever_win_47
    * No Controls
    local c "stratum_win_prob c.stratum_win_prob#i.batch"
    iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * With Controls
    if "`y'" != "job_search_online" {
        replace `y'_21 = 0 if batch_after_s21 ==0
        local c "stratum_win_prob c.stratum_win_prob#i.batch `y'_21 batch_after_s21"
    } 
    if "`y'" == "job_search_online" local c "stratum_win_prob c.stratum_win_prob#i.batch batch_after_s21"

    iv_reg `y'_ivc_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * Add models to list
    global model_list $model_list `y'_iva_22 `y'_ivc_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA32.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/TableA32" "depress_disint_n"

/*----------------------------------------------------------------*/
* Section: 2022 Assets IV Regressions
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22 ivc_22
global column_num 2

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_assets

* loop through each variable of table
foreach y of varlist $vars {

    replace x = ever_win_47
    * No Controls
    local c "stratum_win_prob c.stratum_win_prob#i.batch"
    iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * With Controls
    replace `y'_21 = 0 if batch_after_s21 ==0
    local c "stratum_win_prob c.stratum_win_prob#i.batch `y'_21 batch_after_s21"

    iv_reg `y'_ivc_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * Add models to list
    global model_list $model_list `y'_iva_22 `y'_ivc_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA31.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/TableA31" "bike_buy"

/*----------------------------------------------------------------*/
* Section: 2022 Training Regressions, Cash Before Survey
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22
global column_num 1

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_training

* loop through each variable of table
foreach y of varlist $vars {
    
    * Controls Local
    local c "stratum_win_prob c.stratum_win_prob#i.batch"

    * OLS
    if "`y'" == "ever_win_47" {
        replace x = win_in_batch
        ols_reg `y'_iva_22 `y' x "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$vce" // (batch 2-22)
    }

    * IV
    if "`y'" != "ever_win_47" {
        replace x = cash_before_survey
        iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)
    } 

    * Add models to list
    global model_list $model_list `y'_iva_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA17.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/TableA17" "ever_win_47"

/*----------------------------------------------------------------*/
* Section: 2022 Main IV Regressions, Cash Before Survey
/*----------------------------------------------------------------*/

* Declare wave-specific specifications
global columns iva_22 ivc_22
global column_num 2

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars $vars_22_main

* loop through each variable of table
foreach y of varlist $vars {

    replace x = cash_before_survey
    * No Controls
    local c "stratum_win_prob c.stratum_win_prob#i.batch"
    iv_reg `y'_iva_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * With Controls
    replace `y'_21 = 0 if batch_after_s21 ==0
    local c "stratum_win_prob c.stratum_win_prob#i.batch `y'_21 batch_after_s21"

    iv_reg `y'_ivc_22 `y' x "$instrument_iv" "`c'" "round == 2022 & $after_batch_18" "$demog_batch" "$cluster" // (batch 2-22)

    * Add models to list
    global model_list $model_list `y'_iva_22 `y'_ivc_22
    * Add var to list
    global var_list $var_list `y'
}

* Flip table and export table
save_sterfile "$KP_output/tables/impacts/sterfiles/TableA18.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table_online "$columns" "$KP_output/tables/impacts/tex/TableA18" "tot_cons_adj"